<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="电影名称" prop="movieName">
        <el-input v-model="queryParams.movieName" placeholder="请输入电影名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="上映时间" prop="releaseDate">
        <el-date-picker clearable v-model="queryParams.releaseDate" type="date" value-format="YYYY-MM-DD"
          placeholder="请选择上映时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="总票房" prop="movieBoxOffice">
        <el-input v-model="queryParams.movieBoxOffice" placeholder="请输入电影总票房" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['cine:movie:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['cine:movie:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['cine:movie:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['cine:movie:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="movieList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="电影" align="center" prop="movieName" />
      <el-table-column label="时长(单位: 分钟)" align="center" prop="movieLength" />
      <el-table-column label="海报" align="center" prop="moviePoster" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.moviePoster" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="区域" align="center" prop="movieArea" />
      <el-table-column label="上映时间" align="center" prop="releaseDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.releaseDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="总票房" align="center" prop="movieBoxOffice" />
      <el-table-column label="简介" align="center" prop="movieIntroduction" show-overflow-tooltip>
        <template #default="scope">
          <span v-html="scope.row.movieIntroduction"></span>
        </template>
      </el-table-column>
      <el-table-column label="图集" align="center" prop="moviePictures" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.moviePictures" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['cine:movie:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['cine:movie:remove']">删除</el-button>
          <el-button link type="primary" icon="el-icon-view" @click="handleInfo(scope.row)"
            v-hasPermi="['campus:content:query']">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改电影信息管理对话框 -->
    <el-dialog :title="title" v-model="open" width="900px" append-to-body>
      <el-form ref="movieRef" :model="form" :rules="rules" label-width="120px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="电影名称" prop="movieName">
              <el-input v-model="form.movieName" placeholder="请输入电影名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="时长(分钟)" prop="movieLength">
              <el-input v-model="form.movieLength" placeholder="请输入电影时长(单位: 分钟)" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="海报" prop="moviePoster">
              <image-upload v-model="form.moviePoster" :limit="1" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="上映时间" prop="releaseDate">
              <el-date-picker clearable v-model="form.releaseDate" type="date" value-format="YYYY-MM-DD"
                placeholder="请选择上映时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="总票房" prop="movieBoxOffice">
              <el-input v-model="form.movieBoxOffice" placeholder="请输入电影总票房" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="区域" prop="movieArea">
              <el-input v-model="form.movieArea" placeholder="请输入电影区域" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="简介">
              <editor v-model="form.movieIntroduction" :min-height="192" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="图集" prop="moviePictures">
              <image-upload v-model="form.moviePictures" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 调度电影信息详情 -->
    <el-dialog title="电影信息详细" v-model="showInfo" width="900px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="电影名称">{{ form.movieName }}</el-descriptions-item>
        <el-descriptions-item label="时长">{{ form.movieLength }} 分钟</el-descriptions-item>
        <el-descriptions-item label="海报">
          <image-preview :src="form.moviePoster" :width="100" :height="150" />
        </el-descriptions-item>
        <el-descriptions-item label="上映时间">{{ parseTime(form.releaseDate, '{y}-{m}-{d}') }}</el-descriptions-item>
        <el-descriptions-item label="总票房">{{ form.movieBoxOffice }}</el-descriptions-item>
        <el-descriptions-item label="简介">
          <div style="white-space: pre-wrap;"><span v-html="form.movieIntroduction"></span></div>
        </el-descriptions-item>
        <el-descriptions-item label="图集">
          <image-preview v-for="(pic, index) in parseMoviePictures(form.moviePictures)" :key="index" :src="pic"
            :width="100" :height="150" style="margin-right: 10px;" />
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script setup name="Movie">
import { listMovie, getMovie, delMovie, addMovie, updateMovie } from "@/api/cine/movie";

const { proxy } = getCurrentInstance();

const movieList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
//是否显示详细的弹出层
const showInfo = ref(false);


const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    movieName: null,
    movieLength: null,
    moviePoster: null,
    movieArea: null,
    releaseDate: null,
    movieBoxOffice: null,
    movieIntroduction: null,
    moviePictures: null,
    delState: null
  },
  rules: {
    movieName: [
      { required: true, message: "电影名称不能为空", trigger: "blur" }
    ],
    movieIntroduction: [
      { required: true, message: "电影简介不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询电影信息管理列表 */
function getList() {
  loading.value = true;
  listMovie(queryParams.value).then(response => {
    movieList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    movieId: null,
    movieName: null,
    movieLength: null,
    moviePoster: null,
    movieArea: null,
    releaseDate: null,
    movieBoxOffice: null,
    movieIntroduction: null,
    moviePictures: null,
    delState: null
  };
  proxy.resetForm("movieRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.movieId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加电影信息管理";
}

/** 详细按钮操作 */
function handleInfo(row) {
  const _id = row.movieId;
  getMovie(_id).then(response => {
    form.value = response.data;
    console.log(form.value);
    showInfo.value = true;
  });
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _movieId = row.movieId || ids.value
  getMovie(_movieId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改电影信息管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["movieRef"].validate(valid => {
    if (valid) {
      if (form.value.movieId != null) {
        updateMovie(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addMovie(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _movieIds = row.movieId || ids.value;
  proxy.$modal.confirm('是否确认删除电影信息管理编号为"' + _movieIds + '"的数据项？').then(function () {
    return delMovie(_movieIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('cine/movie/export', {
    ...queryParams.value
  }, `movie_${new Date().getTime()}.xlsx`)
}

function parseMoviePictures(pictures) {
  if (!pictures) return [];
  if (Array.isArray(pictures)) return pictures;
  if (typeof pictures === 'string') {
    return pictures.split(',').map(pic => pic.trim());
  }
  return [];
}

getList();
</script>
